<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>放大镜</title>
  <link rel="stylesheet" href="./css/base.css">
  <style>
    main {
      width: 80vw;
      margin: 10px auto;
      position: relative;
    }

    main>.preimg {
      width: 400px;
      height: 400px;
      position: relative;
      border: 1px solid red;
    }

    main>.mask {
      display: none;
      width: 300px;
      height: 300px;
      position: absolute;
      top: 0;
      left: 0;
      background-color: #FEDE4F;
      opacity: .5;
      cursor: move;
      border: 1px solid #cccccc;
    }

    main>.big {
      display: none;
      width: 500px;
      height: 500px;
      background-color: pink;
      position: absolute;
      top: 0px;
      left: 410px;
      z-index: 999;
    }
  </style>
</head>

<body>
  <main>
    <figure class="preimg">
      <img style="width: 150px; height: 150px;" src="https://picsum.photos/500/500?random=4">
    </figure>
    <div class="mask"></div>
    <figure class="big">
      <img src="https://picsum.photos/500/500?random=4">
    </figure>
  </main>
</body>
<script>
  "use strict";
  document.addEventListener("DOMContentLoaded", () => {
    const main = document.querySelector("main");
    const figure = document.querySelector(".preimg");
    const mask = document.querySelector(".mask");
    const big = document.querySelector(".big");

    main.addEventListener("mouseenter", () => {
      mask.style.display = "block";
      big.style.display = "block";
    });
    main.addEventListener("mouseleave", () => {
      mask.style.display = "none";
      big.style.display = "none";
    });
  });
</script>

</html>